'use strict';

module.exports = () => {
  return async function jwtErr(ctx, next) {
    console.log(ctx.app.config.jwt, 'options');
    const token = ctx.request.headers.authority;
    if (token) {
      try {
        // 解密token
        const user = ctx.app.jwt.verify(token, ctx.app.config.jwt.session_key);
        console.log(user);
        await next();
      } catch (error) {
        ctx.body = {
          message: 'token已经过期',
          code: 401,
        };
        return;
      }
    } else {
      ctx.body = {
        msg: '没有拿到token哦',
        code: 401,
      };
      return;
    }

  };
};
